package cn.rongdao.jrkabao.ui;

import android.accounts.NetworkErrorException;

import com.mifengkong.frtools.system.handler.FRMainLooperHandler;

import java.net.ConnectException;
import java.net.UnknownHostException;

import cn.rongdao.jrkabao.R;
import cn.rongdao.jrkabao.base.BaseActivity;
import cn.rongdao.jrkabao.model.main.tab.TabModel;
import cn.rongdao.jrkabao.ui.login.LoginActivity;
import cn.rongdao.jrkabao.ui.main.MainActivity;
import cn.rongdao.jrkabao.util.LoginUtil;


/**
 * Created by jack on 17/5/9
 * 欢迎页
 */

public class StartUpActivity extends BaseActivity {

    @Override
    protected int layoutRes() {
        return R.layout.activity_splash;
    }

    @Override
    protected void initView() {
        initTabData();
    }

    private void initTabData() {
        TabModel.getInstance().setTab().setOnTabRequestListener(new TabModel.OnTabRequestListener() {
            @Override
            public void success() {
                FRMainLooperHandler.getInstance().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        toLogin();
                    }
                }, 500);
            }

            @Override
            public void failure(Throwable t) {
                if (t instanceof NetworkErrorException
                        || t instanceof UnknownHostException
                        || t instanceof ConnectException) {
                    NoNetWorkActivity.start(thisActivity());
                    finish();
                } else {
                    toLogin();
                }
            }
        });
    }

    private void toLogin() {
        if (LoginUtil.isLogin()) {
            MainActivity.Companion.start(thisActivity());
        } else {
            LoginActivity.start(thisActivity());
        }
        finish();
    }
}